package chinatelecom.feilong.license.manager;

import chinatelecom.feilong.license.constant.LicenseConstant;
import com.ideal.license.core.model.HardWarePercent;
import com.ideal.license.core.model.LicenseModel;
import com.ideal.license.signer.manager.LicenseSigner;
import com.ideal.license.verifier.manager.LicenseVerify;
import com.ideal.license.verifier.manager.VerifyModel;

public class LicenseManager {

    /**
     * 生成license.
     * @param model
     * @return
     * @throws Exception
     */
    public String createLicense(LicenseModel model) throws Exception {
        CheckModelParam.checkParam(model);
        this.initLicenseModel(model);
        LicenseSigner signer = new LicenseSigner();
        return signer.createLicense(model);
    }

    /**
     * 验证license.
     * @param vmodel
     * @return
     * @throws Exception
     */
    public LicenseModel verfy(VerifyModel vmodel) throws Exception {
        CheckModelParam.checkVerifierModel(vmodel);
        this.initVerifierModel(vmodel);
        LicenseVerify verify = new LicenseVerify();
        LicenseModel model = verify.verfiyServer(vmodel);

        return model;
    }

    /**
     * 初始化licenseModel.
     * @param model
     */
    private void initLicenseModel(LicenseModel model) {
        if (LicenseConstant.NORAML_EDITION.equals(model.getEdition())) {
            model.setKeyAlg("feilong");
        } else if (LicenseConstant.ENTERPRISE_EDITION.equals(model.getEdition())) {
            model.setKeyAlg("feilong-qiye");
        }

        model.setKeyAlg(LicenseConstant.RSA);
        model.setConsumerAmount(1);
        model.setConsumerType("user");
    }

    /**
     * 初始化 verifierModel.
     * @param model
     */
    private void initVerifierModel(VerifyModel model) {

        if (LicenseConstant.NORAML_EDITION.equals(model.getEdition())) {
            model.setAlias("feilong");
        } else if (LicenseConstant.ENTERPRISE_EDITION.equals(model.getEdition())) {
            model.setAlias("feilong-qiye");
        }

        model.setKeyAlg(LicenseConstant.RSA);
    }

}
